#include "pagenavigator.h"
#include "ui_pagenavigator.h"
#include <QDebug>

PageNavigator::PageNavigator(QWidget *parent) : QWidget(parent),
                                                ui(new Ui::PageNavigator)
{
    ui->setupUi(this);

    m_maxPage = 1;
    m_curPage = 1;
    // 自带图标
    //  QIcon arrowIcon = ui->tBtnFirst->style()->standardIcon(QStyle::SP_MediaVolumeMuted);
    //  ui->tBtnFirst->setIcon(arrowIcon);

    QStringList strList;
    strList << "10"
            << "20"
            << "50"
            << "100"
            << "500"
            << "1000";
    ui->pagePerCount->addItems(strList);

    ui->tBtnSwitch->setProperty("state", "enable");

    UpdateButtonState();
    ui->pagePerCount->setCurrentIndex(1);
    connect(ui->pagePerCount, &QComboBox::currentTextChanged, this, &PageNavigator::SigPageLinesChanged);

    setStyleSheet(R"(
    QToolButton {
            color: white;
            background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, 
                                            stop:0 #2C5F9D, stop:1 #1A3F7A);
            border: 1px solid #4A7EBB;
            border-radius: 5px;
    }
    QToolButton[state="enable"] {
            background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, 
                                            stop:0 #2C5F9D, stop:1 #1A3F7A);
    }

    QToolButton:hover {
        background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, 
                                        stop:0 #3D7CC9, stop:1 #2A5CA6);
        border-color: #6BA3E0;
    }
    QToolButton[state="disable"]{
        color: rgb(255, 255, 255);
        border-radius: 5px;
        background-color:#5a5a5a;
    }
)");
}

PageNavigator::~PageNavigator()
{
    delete ui;
}

void PageNavigator::updateNavbar(int total, int pageSize)
{
    // 处理总页数为0的情况
    m_totalRecord = total; // 保持总记录数
    ui->labelTotalRecord->setText(QString("共有%1条记录").arg(total));
    m_maxPage = (total + pageSize - 1) / pageSize;
    if (m_maxPage == 0)
        m_maxPage = 1;

    // 确保当前页不超过最大页
    if (m_curPage > m_maxPage)
    {
        m_curPage = m_maxPage;
    }

    // 适配pageSize参数, 每页显示行数 pagePerCount 采用固定枚举值
    int index = 0;
    int selectCount = ui->pagePerCount->count();

    if (pageSize < ui->pagePerCount->itemText(0).toInt())
    {
        index = 0;
    }
    if (pageSize >= ui->pagePerCount->itemText(selectCount - 1).toInt())
    {
        index = selectCount - 1;
    }
    for (int i = 0; i < ui->pagePerCount->count(); i++)
    {
        if (pageSize >= ui->pagePerCount->itemText(i).toInt() &&
            pageSize < ui->pagePerCount->itemText(i + 1).toInt())
        {
            index = i;
        }
    }

    ui->pagePerCount->setCurrentIndex(index);
    ui->labelCurPage->setText(QString("第%1页/共%2页").arg(m_curPage).arg(m_maxPage));
    UpdateButtonState();
}

void PageNavigator::ResetNavBar()
{
    m_totalRecord = 0;
    ui->labelTotalRecord->setText(QString("共有%1条记录").arg(m_totalRecord));

    ui->pagePerCount->setCurrentIndex(0);
    m_pageLines = ui->pagePerCount->itemText(0).toInt();

    ui->lineEditPage->clear();

    m_maxPage = 1;
    m_curPage = 0;
    SetCurrentPage(1);
}

void PageNavigator::SetCurrentPage(int page)
{
    if (m_curPage == page)
        return;

    if (page < 1)
        page = 1;
    if (page > m_maxPage)
        page = m_maxPage;

    m_curPage = page;
    ui->labelCurPage->setText(QString("第%1页/共%2页").arg(m_curPage).arg(m_maxPage));
    UpdateButtonState();

    emit SigCurrentPageChanged(m_curPage);
}
void PageNavigator::setLabelTotalRecord(int total)
{
    ui->labelTotalRecord->setText(QString("共有%1条记录").arg(total));
}
void PageNavigator::HandlePageLinesChanged(int count)
{
    if (m_pageLines == count)
        return;

    m_pageLines = count;
    m_maxPage = (m_totalRecord + (m_pageLines - 1)) / m_pageLines;

    // 重置到第一页并触发刷新
    m_curPage = 1;
    UpdateButtonState();
}
void PageNavigator::UpdateButtonState()
{
    if (m_curPage == 1 && m_maxPage == 1)
    {
        // 单页显示 single page
        ui->tBtnFirst->setEnabled(false);
        ui->tBtnFirst->setProperty("state", "disable");
        ui->tBtnLast->setEnabled(false);
        ui->tBtnLast->setProperty("state", "disable");
        ui->tBtnPrevious->setEnabled(false);
        ui->tBtnPrevious->setProperty("state", "disable");
        ui->tBtnNext->setEnabled(false);
        ui->tBtnNext->setProperty("state", "disable");
        UpdateButtonQss();
    }
    else if (m_curPage == 1 && m_maxPage > 1)
    {
        // 首页显示 first page
        ui->tBtnFirst->setEnabled(false);
        ui->tBtnFirst->setProperty("state", "disable");
        ui->tBtnLast->setEnabled(true);
        ui->tBtnLast->setProperty("state", "enable");
        ui->tBtnPrevious->setEnabled(false);
        ui->tBtnPrevious->setProperty("state", "disable");
        ui->tBtnNext->setEnabled(true);
        ui->tBtnNext->setProperty("state", "enable");
        UpdateButtonQss();
    }
    else if (m_curPage == m_maxPage)
    {
        // 尾页显示 last page
        ui->tBtnFirst->setEnabled(true);
        ui->tBtnFirst->setProperty("state", "enable");
        ui->tBtnLast->setEnabled(false);
        ui->tBtnLast->setProperty("state", "disable");
        ui->tBtnPrevious->setEnabled(true);
        ui->tBtnPrevious->setProperty("state", "enable");
        ui->tBtnNext->setEnabled(false);
        ui->tBtnNext->setProperty("state", "disable");
        UpdateButtonQss();
    }
    else
    {
        // 中间页 mid page
        ui->tBtnFirst->setEnabled(true);
        ui->tBtnFirst->setProperty("state", "enable");
        ui->tBtnLast->setEnabled(true);
        ui->tBtnLast->setProperty("state", "enable");
        ui->tBtnPrevious->setEnabled(true);
        ui->tBtnPrevious->setProperty("state", "enable");
        ui->tBtnNext->setEnabled(true);
        ui->tBtnNext->setProperty("state", "enable");
        UpdateButtonQss();
    }
}

void PageNavigator::UpdateButtonQss()
{
    ui->tBtnFirst->style()->unpolish(ui->tBtnFirst);
    ui->tBtnFirst->style()->polish(ui->tBtnFirst);
    ui->tBtnFirst->update();
    ui->tBtnLast->style()->unpolish(ui->tBtnLast);
    ui->tBtnLast->style()->polish(ui->tBtnLast);
    ui->tBtnLast->update();
    ui->tBtnPrevious->style()->unpolish(ui->tBtnPrevious);
    ui->tBtnPrevious->style()->polish(ui->tBtnPrevious);
    ui->tBtnPrevious->update();
    ui->tBtnNext->style()->unpolish(ui->tBtnNext);
    ui->tBtnNext->style()->polish(ui->tBtnNext);
    ui->tBtnNext->update();
}

void PageNavigator::on_pagePerCount_currentIndexChanged(const QString &arg1)
{
    HandlePageLinesChanged(arg1.toInt());
}

void PageNavigator::on_tBtnSwitch_clicked()
{
    if (ui->lineEditPage->text().isEmpty())
    {
        return;
    }

    SetCurrentPage(ui->lineEditPage->text().toInt());
}

void PageNavigator::on_tBtnPrevious_clicked()
{
    SetCurrentPage(m_curPage - 1);
}

void PageNavigator::on_tBtnNext_clicked()
{
    SetCurrentPage(m_curPage + 1);
}

void PageNavigator::on_tBtnFirst_clicked()
{
    SetCurrentPage(1);
}

void PageNavigator::on_tBtnLast_clicked()
{
    SetCurrentPage(m_maxPage);
}